#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <ST73XX_UI.h>
#include <ST7305_1p54_BW_DisplayDriver.h>
#include <U8g2_for_ST73XX.h>

#define DC_PIN   7
#define RES_PIN  6
#define CS_PIN   10
#define SCLK_PIN 2
#define SDIN_PIN 3

ST7305_1p54_BW_DisplayDriver display(DC_PIN, RES_PIN, CS_PIN, SCLK_PIN, SDIN_PIN, SPI);
U8G2_FOR_ST73XX u8g2_for_st73xx;

#define splash2_width  82
#define splash2_height 64

const uint8_t PROGMEM splash2_data[] = {
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000001,0b10000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000011,0b10000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000111,0b11000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000111,0b11000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00001111,0b11000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00011111,0b11100000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00011111,0b11100000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00111111,0b11100000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00111111,0b11110000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b01111111,0b11110000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00011111,0b11111000,0b01111111,0b11110000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00111111,0b11111110,0b01111111,0b11110000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00111111,0b11111111,0b01111111,0b11110000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00011111,0b11111111,0b11111011,0b11100000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00001111,0b11111111,0b11111001,0b11111111,0b11000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00001111,0b11111111,0b11111001,0b11111111,0b11111000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000111,0b11111111,0b11110001,0b11111111,0b11111111,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000011,0b11111100,0b01110011,0b11111111,0b11111111,0b10000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000001,0b11111110,0b00111111,0b11111111,0b11111111,0b10000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b11111111,0b00011110,0b00001111,0b11111111,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b01111111,0b11111110,0b00011111,0b11111100,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00111111,0b11111111,0b11111111,0b11111000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00001111,0b11011111,0b11111111,0b11100000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00011111,0b00011001,0b11111111,0b11000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00111111,0b00111100,0b11111111,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b01111110,0b01111100,0b11111000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b01111111,0b11111110,0b01111100,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b11111111,0b11111111,0b11111100,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b11111111,0b11111111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b11111111,0b11111111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000001,0b11111111,0b11101111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000001,0b11111111,0b11001111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000011,0b11111111,0b00000111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000011,0b11111100,0b00000111,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000011,0b11110000,0b00000011,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000001,0b10000000,0b00000000,0b11111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b01111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00111110,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00001100,0b00000000,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000111,0b10000000,0b00000000,0b11111100,0b00000000,0b00000000,0b00000011,0b11000000,0b00000000,
  0b00000000,0b00000000,0b00000111,0b10000000,0b00000001,0b11111100,0b00000000,0b00000000,0b00000011,0b11000000,0b00000000,
  0b00000000,0b00000000,0b00000111,0b10000000,0b00000001,0b11111100,0b00000000,0b00000000,0b00000011,0b11000000,0b00000000,
  0b00000000,0b00000000,0b00000111,0b10000000,0b00000001,0b11100000,0b00000000,0b00000000,0b00000000,0b00011110,0b00000000,
  0b00000000,0b00000000,0b00000111,0b10000000,0b00000001,0b11100000,0b00000000,0b00000000,0b00000000,0b00011110,0b00000000,
  0b01111111,0b11100011,0b11110111,0b10011111,0b11111001,0b11111101,0b11100111,0b01111000,0b01111011,0b11011111,0b11000000,
  0b11111111,0b11110111,0b11111111,0b10111111,0b11111101,0b11111101,0b11111111,0b01111000,0b01111011,0b11011111,0b11000000,
  0b11111111,0b11110111,0b11111111,0b10111111,0b11111101,0b11111101,0b11111111,0b01111000,0b01111011,0b11011111,0b11000000,
  0b11110000,0b11110111,0b10000111,0b10111100,0b00111101,0b11100001,0b11111111,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11110000,0b11110111,0b10000111,0b10111100,0b00111101,0b11100001,0b11110000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b00000000,0b11110111,0b10000111,0b10000000,0b00111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b01111111,0b11110111,0b10000111,0b10011111,0b11111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11111111,0b11110111,0b10000111,0b10111111,0b11111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11110000,0b11110111,0b10000111,0b10111100,0b00111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11110000,0b11110111,0b10000111,0b10111100,0b00111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11110000,0b11110111,0b10000111,0b10111100,0b00111101,0b11100001,0b11100000,0b01111000,0b01111011,0b11011110,0b00000000,
  0b11111111,0b11110111,0b11111111,0b10111111,0b11111101,0b11100001,0b11100000,0b01111111,0b11111011,0b11011111,0b11000000,
  0b11111111,0b11110111,0b11111111,0b10111111,0b11111101,0b11100001,0b11100000,0b01111111,0b11111011,0b11011111,0b11000000,
  0b01111100,0b11110011,0b11110011,0b10011111,0b00111101,0b11100001,0b11100000,0b00111110,0b01111011,0b11001111,0b11000000,
  0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,
  0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11111111,0b11000000,
  0b11111111,0b11111111,0b11111111,0b11111111,0b11111101,0b01101000,0b11011011,0b00010001,0b00011010,0b00110001,0b11000000,
  0b11111111,0b11111111,0b11111111,0b11111111,0b11111101,0b00101011,0b01011010,0b11111011,0b01101010,0b11101111,0b11000000,
  0b11111111,0b11111111,0b11111111,0b11111111,0b11111101,0b01001011,0b01011011,0b00111011,0b00011010,0b00110011,0b11000000,
  0b11111111,0b11111111,0b11111111,0b11111111,0b11111101,0b01101011,0b01011011,0b11011011,0b01101010,0b11111101,0b11000000,
};


void setup() {
    Serial.begin(115200);
    Serial.println("Hello Arduino! (V8.0.X)");

    display.initialize();
    // display.Low_Power_Mode();
    display.High_Power_Mode();
    display.display_on(true);
    display.display_Inversion(false);

    u8g2_for_st73xx.begin(display);                 // connect u8g2 procedures to Adafruit GFX

    for(int i = 0; i < 12; i++) {
        display.clearDisplay();
        display.fill(i%2 ? 0xFF : 0);
        display.display();
        delay(300);
    }


    for(int i = 0; i < 4; i++) {
        display.setRotation(i);
        display.clearDisplay();
        display.drawBitmap(
            (display.width()  - splash2_width ) / 2,
            (display.height() - splash2_height) / 2,
            splash2_data, splash2_width, splash2_height, 1);
        display.fillTriangle(20,20, 50,20, 20, 50, 1);
        display.drawRoundRect(10, 10, 180, 180, 8, 1);
        display.display();
        delay(1000);
    }

    // display.setRotation(0);

    display.clearDisplay();

    // 画一条的直线
    display.drawLine(0, 20, 30, 70, ST7305_COLOR_BLACK);

    // 画一个三角形
    display.drawTriangle(40, 5, 60, 35, 80, 5, ST7305_COLOR_BLACK);

    // 画一个实心三角形
    display.drawFilledTriangle(0, 5, 20, 35, 40, 5, ST7305_COLOR_BLACK);

    // 画一个矩形
    display.drawRectangle(70, 40, 120, 70, ST7305_COLOR_BLACK);

    // 画一个实心矩形
    display.drawFilledRectangle(70, 40, 100, 60, ST7305_COLOR_BLACK);

    // 画一个圆形
    display.drawCircle(90, 120, 25, ST7305_COLOR_BLACK);

    // 画一个实心圆形
    display.drawFilledCircle(90, 120, 15, ST7305_COLOR_BLACK);

    // 画一个多边形
    uint polygonPoints1[] = {0, 50, 60, 40, 60, 90, 20, 110, 0, 90};
    display.drawPolygon(polygonPoints1, 5, ST7305_COLOR_BLACK);

    // 画一个实心多边形
    uint polygonPoints2[] = {0, 120, 60, 110, 60, 160, 20, 180, 0, 160};
    display.drawFilledPolygon(polygonPoints2, 5, ST7305_COLOR_BLACK);

    display.display();

    delay(3000);
}

void loop() {
    long time = millis();

    display.setRotation((millis()/1000)%4);

    char str[100];
    static int fps = 0;

    sprintf(str, "millis=%.3fs", ((float)time)/1000.00);

    display.clearDisplay();

    u8g2_for_st73xx.setFontDirection(0);            // left to right (this is default)
    u8g2_for_st73xx.setForegroundColor(ST7305_COLOR_BLACK);      // apply Adafruit GFX color
    u8g2_for_st73xx.setBackgroundColor(ST7305_COLOR_WHITE);
    u8g2_for_st73xx.setFont(u8g2_font_helvR14_tf);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2_for_st73xx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
    u8g2_for_st73xx.setCursor(10,20);                // start writing at this position
    u8g2_for_st73xx.print(str);
    
    u8g2_for_st73xx.setFont(u8g2_font_wqy16_t_gb2312b);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2_for_st73xx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
    u8g2_for_st73xx.setCursor(10,40);                // start writing at this position
    u8g2_for_st73xx.print(F("鱼鹰光电"));

    u8g2_for_st73xx.setFont(u8g2_font_fub42_tn);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2_for_st73xx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
    u8g2_for_st73xx.setCursor(10,100);                // start writing at this position
    static uint8_t min = 0;
    u8g2_for_st73xx.printf("20:%02d", min>59 ? min=0: min++);

    u8g2_for_st73xx.setFontDirection(1);            // left to right (this is default)
    u8g2_for_st73xx.setForegroundColor(ST7305_COLOR_BLACK);      // apply Adafruit GFX color
    u8g2_for_st73xx.setBackgroundColor(ST7305_COLOR_WHITE);
    u8g2_for_st73xx.setFont(u8g2_font_wqy16_t_gb2312b);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2_for_st73xx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
    u8g2_for_st73xx.setCursor(170,10);                // start writing at this position
    u8g2_for_st73xx.print(F("1.54' Mono LCD"));

    u8g2_for_st73xx.setFontDirection(0);            // left to right (this is default)
    u8g2_for_st73xx.setFont(u8g2_font_wqy16_t_gb2312b);  // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
    u8g2_for_st73xx.setFontMode(1);                 // use u8g2 transparent mode (this is default)
    u8g2_for_st73xx.setCursor(10,150);                // start writing at this position
    u8g2_for_st73xx.printf("FPS=%dHz(spi write)", fps);
    u8g2_for_st73xx.setCursor(10,170);                // start writing at this position
    u8g2_for_st73xx.printf("FPS=51Hz(real)");

    display.display();

    fps = 1000.0/(millis() - time);
    Serial.printf("display time = %dms\n", millis() - time);

    delay(20);
}
